package com.ysd.lis.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysd.common.Result;
import com.ysd.lis.entity.SysUserRptGroup;
import com.ysd.lis.mapper.sys.SysUserRptGroupMapper;
import com.ysd.lis.service.SysUserRptGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 用户报告单元关系表 服务实现类
 * </p>
 *
 * @author HuiXing
 * @since 2024-01-02
 */
@Service
public class SysUserRptGroupServiceImpl extends ServiceImpl<SysUserRptGroupMapper, SysUserRptGroup> implements SysUserRptGroupService {

    @Autowired
    private SysUserRptGroupMapper sysUserRptGroupMapper;

    public Result findSysUserRptGroupList(String userId) {
        LambdaQueryWrapper<SysUserRptGroup> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysUserRptGroup::getUserId, userId);
        lambdaQueryWrapper.eq(SysUserRptGroup::getDelFlag, 0);
        List<SysUserRptGroup> sysUserRptGroups = sysUserRptGroupMapper.selectList(lambdaQueryWrapper);
        return Result.succ(1, "查询成功", sysUserRptGroups);
    }

    @Override
    public Result addSysUserRptGroup(SysUserRptGroup sysUserRptGroup) {
        boolean save = this.save(sysUserRptGroup);
        if (save) {
            return Result.succ(1, "添加成功", save);
        } else {
            return Result.fail("添加成功");
        }
    }

    @Override
    public Result addBatchSysUserRptGroup(List<SysUserRptGroup> sysUserRptGroups) {
        boolean save = this.saveBatch(sysUserRptGroups);
        if (save) {
            return Result.succ(1, "添加成功", save);
        } else {
            return Result.fail("添加成功");
        }
    }

    @Override
    public Result deleteSysUserRptGroup(SysUserRptGroup sysUserRptGroup) {
        LambdaQueryWrapper<SysUserRptGroup> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(StrUtil.isNotEmpty(sysUserRptGroup.getRptGroupId()),SysUserRptGroup::getRptGroupId, sysUserRptGroup.getRptGroupId());
        lambdaQueryWrapper.eq(StrUtil.isNotEmpty(sysUserRptGroup.getUserId()),SysUserRptGroup::getUserId, sysUserRptGroup.getUserId());
        int delete = sysUserRptGroupMapper.delete(lambdaQueryWrapper);
        if (delete >= 0) {
            return Result.succ(1, "添加成功", delete);
        } else {
            return Result.fail("添加成功");
        }
    }
}
